//
//  GAKKIBaseTableViewCell.m
//  GANGAIKit
//
//  Created by fh on 2019/11/6.
//  Copyright © 2019 cashchina. All rights reserved.
//

#import "GAKKIBaseTableViewCell.h"

@implementation GAKKIBaseTableViewCell
#pragma mark - Public
/// 快速创建可重用的cell工厂类方法:子类调用,使用类名作为标识符
/// @param tableView tableView
+ (instancetype)dequeueReusableCellWithTable:(UITableView *)tableView {
    return [self dequeueReusableCellWithTable:tableView reusableIdentifier:nil shouldAutoSetupSubViews:YES];
}

/// 快速创建可重用的cell工厂类方法:子类调用
/// @param tableView tableView
/// @param identifier 重用标识符
/// @param shouldAutoSetupSubViews 是否自动调用setupView进行布局
+ (instancetype)dequeueReusableCellWithTable:(UITableView *)tableView reusableIdentifier:(NSString * _Nullable)identifier shouldAutoSetupSubViews:(BOOL)shouldAutoSetupSubViews {
    NSString *cellName = [NSString stringWithUTF8String:object_getClassName(self)];
    Class newclass = NSClassFromString(cellName);
    if ([newclass isSubclassOfClass:[GAKKIBaseTableViewCell class]]) {
        newclass = NSClassFromString(cellName);
    }else {
        newclass = [self class];
    }
    NSString *reusableIdentifier = nil;
    
    BOOL ifNullResult = (identifier == nil || [identifier isKindOfClass:[NSNull class]] || [identifier length] < 1 || [identifier isEqualToString:@"(null)"] || [identifier isEqualToString:@"<null>"] || [identifier isEqualToString:@"<Null>"] ? YES : NO );
    
    if (!ifNullResult) {
        reusableIdentifier = identifier;
    }else {
        reusableIdentifier = cellName;
    }
    GAKKIBaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reusableIdentifier];
    if (cell == nil) {
        cell = [[newclass alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reusableIdentifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;  // 被选中时的背景颜色
    if (shouldAutoSetupSubViews) {
        [cell setupView];
    }
    return cell;
}

/// 配置子视图
- (void)setupView {
    // 子类实现
}

/// 清除所有子视图
- (void)cleanAllSubviews {
    for (UIView *subview in self.contentView.subviews) {
        [subview removeFromSuperview];
    }
}

- (void)dealloc {
    if (kDebugReusableViewRelease) {
#ifdef DEBUG
        DBLog(@"\n ----------- 可重用cell %@ 销毁了 ----------- \n",[self class]);
#endif
    }
}
@end
